import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        System.out.println("Enter task number: ");
        Scanner in = new Scanner(System.in);
        System.out.println("Enter x from -1 to 1:");
        double x = in.nextDouble();
        System.out.println("Enter method of calculation:\n(1)-Iterations;\n(2)-Accuracy;\n(3)-Accuracy eps/10");
        int sw1 = in.nextInt();
        switch (sw1) {
            case 1:
                System.out.println("Enter amount of iterations:");
                int n = in.nextInt();
                OutIntro(x);
                System.out.println("|\ti\t|\t\ta\t|\t\tsum\t\t|");
                double a = 1.0;
                double sum = 0.0;

                for (int i = 0; i < n; i += 2) {
                    a = F(i, a, x);
                    sum += a;
                    System.out.print("|\t" + i + "\t|");
                    System.out.printf("\t%.3f\t|", a);
                    System.out.printf("\t%3.5f\t\t|", sum);
                    System.out.println();
                }
                break;
            case 2:
                System.out.println("Enter accuracy: ");
                double eps = in.nextDouble();
                OutIntro(x, eps);
                a = 1.0;
                sum = 0.0;

                System.out.println("|\ti\t|\t\ta\t|\t\tsum\t\t|");

                FuncEps(x, a, sum, eps);
                break;
            case 3:
                System.out.println("Enter accuracy: ");
                double eps1 = in.nextDouble() / 10;
                OutIntro(x, eps1);
                a = 1.0;
                sum = 0.0;
                System.out.println("|\ti\t|\t\ta\t|\t\tsum\t\t|");

                FuncEps(x, a, sum, eps1);
                break;
        }
    }

    public static boolean Catching(int w) {
        return w % 2 != 0;
    }

    public static void OutIntro(double x) {
        System.out.println("Result of calculation: " + 1.0 / Math.pow(1.0 - x, 2.0));
    }

    public static void OutIntro(double x, double eps) {
        System.out.println("Result of calculation with accuracy eps = " + eps + "equals: " + 1.0 / Math.pow(1.0 - x, 2.0));
    }

    public static double F(int n, double a_n, double x) {
        return (double) calc(n) * Math.pow(x, n);
    }

    public static double calc(int n) {
        if (n == 2)
            return 1 / 2;
        if (n == 0)
            return 1;
        return n - 1 / n * calc(n - 2);
    }


    public static void FuncEps(double x, double a, double sum, double eps) {
        int iter = 0;
        while (a > eps) {
            a = F(iter, a, x);
            sum += a;
            System.out.print("|\t" + iter + "\t|");
            System.out.printf("\t%.5f\t|", a);
            System.out.printf("\t%3.5f\t\t|", sum);
            System.out.println();
            iter++;
        }

    }
}